package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.TzCollectionOrder;
import com.ruoyi.system.service.TzCollectionOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/business/tzCollectionOrder")
@Api(tags = "催款订单管理")
public class TzCollectionOrderController extends BaseController {


    @Autowired
    private TzCollectionOrderService tzCollectionOrderService;

    @GetMapping("/list")
    @ApiOperation("获取催款订单列表")
    public TableDataInfo list(
            @ApiParam("分页信息") PageDomain pageDomain,
            @ApiParam("查询条件") TzCollectionOrder tzCollectionOrder) {
        startPage();
        return getDataTable(tzCollectionOrderService.pageList(pageDomain, tzCollectionOrder));
    }

    @PostMapping("/add")
    @ApiOperation("添加催款订单")
    public R add(@RequestBody TzCollectionOrder tzCollectionOrder) {
        tzCollectionOrderService.save(tzCollectionOrder);
        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("更新催款订单")
    public R update(
            @ApiParam("催款订单信息") @RequestBody TzCollectionOrder tzCollectionOrder) {
        return R.ok(tzCollectionOrderService.update(tzCollectionOrder));
    }


    @PostMapping("/bindUser")
    @ApiOperation("绑定用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "催款订单id", required = true, dataType = "Long"),
//            @ApiImplicitParam(name = "postId", value = "部门id", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "Long"),
    })
    public R bindUser(@RequestParam Long id, @RequestParam Long userId) {
        return R.ok(tzCollectionOrderService.bindUser(id, userId));
    }


    @GetMapping("/userList/{postId}")
    @ApiOperation("根据部门获取用户列表")
    public R<List<SysUser>> userList(@PathVariable ("postId") Long postId) {
        return R.ok(tzCollectionOrderService.getUserList( postId));
    }



    @GetMapping("/getDetail/{id}")
    @ApiOperation("根据id获取订单详细信息----------------->新接口")
    public R<TzCollectionOrder> getDetail(@PathVariable("id") Long id) {
        TzCollectionOrder collectionOrder = tzCollectionOrderService.getById(id);
        return R.ok(collectionOrder);
    }

    @PostMapping("/updateStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "status", value = "催款状态 0-待催收 1-催收中 2-承诺还款  4-催收失败", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "collectionRemark", value = "催款备注", required = true, dataType = "String"),

    })
    @ApiOperation("更新订单催款状态----------------->新接口,form 形式")
    public R updateStatus(@RequestParam Long id, @RequestParam Integer status,@RequestParam String collectionRemark)  {
        TzCollectionOrder collectionOrder = tzCollectionOrderService.getById(id);
        collectionOrder.setCollectionStatus(status);
        collectionOrder.setLastCollectionTime(new Date());
        collectionOrder.setCollectionRemark(collectionRemark);
        tzCollectionOrderService.updateById(collectionOrder);
        return R.ok();
    }



    @PostMapping("/collectionStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "settlementAmount", value = "结算金额", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "成功备注", required = true, dataType = "String"),

    })
    @ApiOperation("催款成功----------------->新接口,form 形式")
    public R getDetail(@RequestParam Long id,  @RequestParam BigDecimal settlementAmount, String remark)   {
        TzCollectionOrder collectionOrder = tzCollectionOrderService.getById(id);
        collectionOrder.setCollectionStatus(4);
        collectionOrder.setLastCollectionTime(new Date());
        collectionOrder.setRemark(remark);
        collectionOrder.setSettlementAmount(settlementAmount);
        tzCollectionOrderService.updateById(collectionOrder);
        return R.ok();
    }

}
